package com.exam.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.exam.dao.ClassDao;
import com.exam.entity.Classes;
import com.exam.entity.User;
import com.exam.util.PageBean;
import com.google.gson.Gson;

/**
 * 用户的所有操作
 */
@WebServlet("/admin/class")
public class ClassServlet extends BaseServlet {
	ClassDao dao = new ClassDao();
	Gson gson=new Gson();
	
	// 查一页数据
	protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("查一页数据");  // ?m=page&p=4
		//乱码处理
		response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        request.setCharacterEncoding("utf-8");
		String p = request.getParameter("p");  // 你要看的页面
		if (p == null) {
			p="1";
		}
		int intp =  Integer.parseInt(p);  // 根据当前页，算出开始结束的页码
		List<Classes> classlist=null;
		try {
			classlist = dao.sel(intp);
			for (Classes classes : classlist) {
				//学生总数
				classes.setCount(dao.count(classes.getClass_id()));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		String data=gson.toJson(classlist);
		PrintWriter out=response.getWriter();
		out.print(data);
	}
	//查班级总数
	protected void count(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		int count=dao.count();
		String json=gson.toJson(count);
		PrintWriter out=response.getWriter();
		out.write(json);
	}
	//查班级学生总数
	protected void countUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		String id = request.getParameter("classid");  // 你要看的页面
		int classid =  Integer.parseInt(id);
		int count=dao.count(classid);
		String json=gson.toJson(count);
		PrintWriter out=response.getWriter();
		out.write(json);
	}

	// ID 查一个班级的数据
	protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("查一个班级的数据");
		response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        request.setCharacterEncoding("utf-8");
		String id = request.getParameter("classid");
		int classid=Integer.parseInt(id);
		String p = request.getParameter("p");  // 你要看的页面
		if (p == null) {
			p="1";
		}
		int intp =  Integer.parseInt(p);  // 根据当前页，算出开始结束的页码
		List<User> userlist=null;
		try {
			userlist = dao.show(intp,classid);  //查这个班级的一页学生数据
			for (User user : userlist) {
				user.setClass_name(dao.selClasses(user.getClass_id()));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 传给JSP  ---》 request
		String json=gson.toJson(userlist);
		PrintWriter out=response.getWriter();
		out.write(json);
	}

	protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
		System.out.println("class add");
		resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        req.setCharacterEncoding("utf-8");
        
        int flag=0;
        System.out.println("创建班级");

        String class_name = req.getParameter("classname");
        
        byte sz[]=class_name.getBytes("iso8859-1");
		String classname = new String(sz,"utf-8");
        
		String class_create = req.getParameter("classdate");
		SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
		Date time=ft.parse(class_create);
		
		String remark = req.getParameter("desc");
		byte sz1[]=remark.getBytes("iso8859-1");
		String class_remark = new String(sz1,"utf-8");
		
        int class_state = Integer.parseInt(req.getParameter("state"));
        
        Classes classes=new Classes(classname, time, class_remark, class_state);

        try {
            flag = dao.registerClass(classes);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("插入失败！。。。");
        }

        if (flag==1){
            System.out.println("注册成功");
            resp.sendRedirect("/OnlineExam1/admin/class_manage.jsp");
        }
	}

	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		System.out.println("class del"+id);
		int classid=0;
		try {
			classid=Integer.parseInt(id);
			dao.del(classid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//去哪？？ 去查第一页
		response.sendRedirect("/OnlineExam1/admin/class_manage.jsp");
		
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("class update");
		String classid = request.getParameter("uid");
		System.out.println(classid);
		int id=Integer.parseInt(classid);
		
		Classes cls=null;
		String time1=null;
		try {
			cls = dao.selClass(id);//查一个班级信息
			Date time=cls.getClass_create();
			SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
			time1=ft.format(time);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 传给JSP  ---》 request
		request.setAttribute("cls", cls);
		request.setAttribute("time", time1);
		request.getRequestDispatcher("/admin/modify_class.jsp").forward(request, response);
	}
	//修改用户后数据处理
	protected void modify(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException, ParseException {
		resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        req.setCharacterEncoding("utf-8");
        
        int flag=0;
        System.out.println("修改班级信息");

        String class_name = req.getParameter("class");
        
        byte sz[]=class_name.getBytes("iso8859-1");
		String classname = new String(sz,"utf-8");
        
		String class_create = req.getParameter("time");
		SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
		Date time=ft.parse(class_create);
		
		String remark = req.getParameter("remark");
		byte sz1[]=remark.getBytes("iso8859-1");
		String class_remark = new String(sz1,"utf-8");
		
        int class_state = Integer.parseInt(req.getParameter("state"));
        
        Classes classes=new Classes(classname, time, class_remark, class_state);
        classes.setClass_id(Integer.parseInt(req.getParameter("id")));
        System.out.println(classes);
        try {
            flag = dao.modifyClass(classes);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("修改失败！。。。");
        }
        System.out.println(flag);
        if (flag==1){
            System.out.println("修改成功");
            resp.sendRedirect("/OnlineExam1/admin/class_manage.jsp");
        }
	}
}
